$(document).ready(function () {

    var cbpAnimatedHeader = (function () {

        var docElem = document.documentElement,
                header = document.querySelector('.navbar-default'),
                didScroll = false,
                changeHeaderOn = 200;

        function init() {
            window.addEventListener('scroll', function (event) {
                if (!didScroll) {
                    didScroll = true;
                    setTimeout(scrollPage, 250);
                }
            }, false);
        }

        function scrollPage() {
            var sy = scrollY();
            if (sy >= changeHeaderOn) {
                classie.add(header, 'navbar-shrink');
            }
            else {
                classie.remove(header, 'navbar-shrink');
            }
            didScroll = false;
        }

        function scrollY() {
            return window.pageYOffset || docElem.scrollTop;
        }

        init();

    })();

    $('a.page-scroll').bind('click', function (event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, 1500, 'easeInOutExpo');
        event.preventDefault();
    });

    // Highlight the top nav as scrolling occurs
    $('body').scrollspy({
        target: '.navbar-fixed-top'
    })

    // Closes the Responsive Menu on Menu Item Click
    $('.navbar-collapse ul li a').click(function () {
        $('.navbar-toggle:visible').click();
    });

    // Closes the Responsive Menu on Click outside Menu
    $('body > *').not('nav').click(function () {
        if (!$('button.navbar-toggle').hasClass('collapsed')) {
            $('.navbar-toggle:visible').click();
        }
    });
    // effects when portrait hover

    $('.portrait-item').hover(function () {

        var obj = $(this);
        obj.find('.teachershow-mask').css('opacity', '0.6');
        obj.find('.teachershow-link').css('display', 'block')
                .delay(2000)
                .css('animation', 'bounceIn .5s ease .2s both');
        obj.find('.teacherinfo').css({
            'background-image': 'none',
            'background-color': 'rgb(205,65,109)'
        });
        obj.find('.teacherinfo').children().css('color', '#fff');
		obj.find('.teacherinfo span').css('color', '#fff');

		var timer = setTimeout(function () {
			obj.find('p').fadeOut(600);
			obj.find('h2').fadeOut(600);
			obj.find('.show-txt').fadeIn(600);
		}, 2500);

		obj.data('timer', timer);
    }, function () {

        var obj = $(this);
        obj.find('.teachershow-mask').css('opacity', '0');
        obj.find('.teachershow-link').css('animation', 'bounceOut .5s ease .2s both');
        obj.find('.teacherinfo').css({
            'background-image': 'url("http://beatsbeans-0.oss-cn-beijing.aliyuncs.com/static_20151014/img/teacher-bg.gif")',
            'background-color': '#f0f0ef'
        });
        obj.find('.teacherinfo').children().css('color', 'rgb(102,102,102)');
		obj.find('.teacherinfo span').css('color', 'rgb(102,102,102)');
		clearTimeout(obj.data('timer'));
		obj.find('p').show();
		obj.find('h2').show();
		obj.find('.show-txt').hide();
    });
});

var fast_order=function (argument) {
	$.ajax({
	    type: 'POST',
	    url: '/temp_info/rest/fast_order/',
	    headers: {
	      'X-CSRFToken': Cookies.get("csrftoken"),
	    },
	    data: {
	    	"phone_number":$("#phone").val(),
	    	"subject":$("#subject").val(),
	    	"time":$("#time").val(),
	    	"location":$("#location").val(),
	    	"desc":$("#desc").val(),
	    	"teacher":"default",
	    },
	    success:function(res){
	        alert("提交预约成功,我们将于24小时内与您联系.");
	    }
	});
}
var get_navbar=function () {
	$.get("/login/navbar/",
		function(data,status){
			$("#navbar").html(data);
	});
};
var get_teacher_list=function (query) {
	window.location.href="/index/teachers/?page="+query.page+"&teach_subject="+query.teach_subject+"&teach_grade="+query.teach_grade+"&name="+query.name+"&school="+query.school+"&gender="+query.gender;
}
var page_i=function (i) {
	query.page=i;
	get_teacher_list(query);
};
var page_previous=function () {
	query.page=query.page==0?0:query.page-1;
	get_teacher_list(query);
};
var page_next=function () {
	query.page=query.page+1;
	get_teacher_list(query);
};
var subject_filter=function (str){
	query.page=0;
	query.teach_subject=str;
	get_teacher_list(query);
};

var grade_filter=function(str){
	query.page=0;
	query.teach_grade=str;
	get_teacher_list(query);
};

var school_filter=function(str){
	query.page=0;
	query.school=str;
	get_teacher_list(query);
};

var gender_filter=function(str){
	query.page=0;
	query.gender=str;
	get_teacher_list(query);
};

var name_filter=function(){
	query.page=0;
	query.name=$("#filter_name").val();
	get_teacher_list(query);
};



// var teach_grade_filter=function (str){
// 	get_teacher_list({"filter_by_teach_grade":str});
// }
var onkeydown_search=function () {
	if(window.event.keyCode == 13){
	    $("#search").click();
	}
}
var recommend_teacher=function () {
	if(!$("#recommend_form").validate()) return false;
	$.ajax({
	    type: 'POST',
	    url: '/temp_info/rest/fast_order/',
	    headers: {
	      'X-CSRFToken': Cookies.get("csrftoken"),
	    },
	    data: {
	    	"phone_number":$("#recommend_phone").val(),
	    	"subject":"",
	    	"time":"",
	    	"location":"",
	    	"desc":$("#recommend_desc").val(),
	    	"teacher":"",
	    },
	    success:function(res){
	        alert("我们极力帮您筛选中,将于24小时内与您联系.");
	    }
	});
}

var green816=function () {
	$.ajax({
	    type: 'POST',
	    url: '/temp_info/rest/fast_order/',
	    headers: {
	      'X-CSRFToken': Cookies.get("csrftoken"),
	    },
	    data: {
	    	"phone_number":$("#contact").val(),
	    	"subject":"绿色行动信息(客服不用管!!)",
	    	"time":"",
	    	"location":"",
	    	"desc":$("#desc").val(),
	    	"teacher":"",
	    },
	    success:function(res){
	        alert("提交成功,谢谢参与.");
	    }
	});
}
